<!DOCTYPE html>

<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Editor</title>

    <link rel="stylesheet" href="styles.css" type="text/css" media="screen" charset="utf-8">
</head>
<body>

    <div id="editor">
		<div id="toggleGutter" onclick="toggleGutter();"></div>
		<div id="reqUI" onclick="stopReq();">777</div>		
	</div>
	<div id="contextMenuBack">
		<div id="contextMenu"></div>
	</div>
    <script type="text/javascript">
//add stub console since ace calls console.error
if(!window.console){
	console={};
	console.log = console.error = function(){};
}

function transformPath(path){
	var aceRoot = '../ace/build/src/'
	var sub = 'ace/theme/'
	if(path.indexOf(sub)==0){
		return aceRoot + 'theme-' + path.substr(sub.length) + '.js'
	}
	var sub = 'ace/keyboard/keybinding/'
	if(path.indexOf(sub)==0){
		return aceRoot + 'keybinding-' + path.substr(sub.length) + '.js'
	}
	var sub = 'ace/mode/'
	if(path.indexOf(sub)==0){
		return aceRoot + 'mode-' + path.substr(sub.length) + '.js'
	}
	var sub = 'fbace/'
	if(path.indexOf(sub)==0){
		return path.substr(sub.length) + '.js'
	}
	var sub = 'shadia/'
	if(path.indexOf(sub)==0){
		return '../' + path.substr(sub.length) + '.js'
	}
	return aceRoot + path + '.js'	
}

require = loadScripts = function(deps, callback, lastUrl){
	//console.log(deps)
	if(typeof deps == 'string')//this happens when module's path is wrong
		return;
		
	var url = deps.shift()
	if(!url)
		return callback&&callback(require(lastUrl))
	
	
	var script = document.createElement("script")
	script.type = "text/javascript;version=1.8";	  
	script.onload = function(){
		this.onload = null
		loadScripts(deps, callback, url)
	}
   
	script.src = transformPath(url);
	document.documentElement.appendChild(script);	
}

//*******************************************************************************************
//mini require from ace doesn't work :(
var _define = function(module, deps, payload) {
    if (arguments.length == 2)
        payload = deps;

	if (typeof module !== 'string') {
        if (_define.original)
            _define.original.apply(window, arguments);
        else 
            console.error('dropping module because define wasn\'t a string.');       
        
		return;
    }
    if(!define.modules[module])    
		define.modules[module] = payload;
};

_define.modules = {};    
if (window.define)
    _define.original = window.define;
window.define = _define;

var _require = function(module, callback) {
    if (Object.prototype.toString.call(module) === "[object Array]") {
        var params = [];
        for (var i = 0, l = module.length; i < l; ++i) {
            var dep = lookup(module[i]);
            if (!dep && _require.original)
                return _require.original.apply(window, arguments);
            params.push(dep);
        }
        if (callback) {
            callback.apply(null, params);
        }
    }
    else if (typeof module === 'string') {
        var payload = lookup(module);
        if (!payload && _require.original)
            return _require.original.apply(window, arguments);
        
        if (callback) {
            callback();
        }
    
        return payload;
    }
    else {
        if (_require.original)
            return _require.original.apply(window, arguments);
    }
};

if (window.require)
    _require.original = window.require;
    
window.require = _require;
require.packaged = true;

var lookup = function(moduleName) {
    var module = define.modules[moduleName];
    if (module == null) {
        //console.error('Missing module: ' + moduleName);
        return null;
    }

    if (typeof module === 'function') {
        var exports = {};
        module(require, exports, { id: moduleName, uri: '' });
        // cache the resulting module object for next time
        define.modules[moduleName] = exports;
        return exports;
    }

    return module;
};
//*********************************************************************************************

var onLaunch
var startAce = function(callback, options) {
	if(onLaunch){
		onLaunch = callback || onLaunch
		return
	}
	onLaunch = callback || function(){};
	// "ace/mode/javascript", "ace/mode/css", are loaded with html  // "ace-uncompressed" for debugging
	var rootDeps = ["fbace/scrollbar", "ace-uncompressed", "ace/mode/xml", "ace/mode/html", "fbace/startup"
		,'shadia/utils/fileUtils', "fbace/imageViewer"
	];
	if(!options)
		options = {};
	//"ace/theme/textmate" is built into ace.js so there's no need to load that
	if(!options.theme)
		options.theme = "ace/theme/textmate";
	else if(options.theme != "ace/theme/textmate")
		rootDeps.push(options.theme);

	require(rootDeps, function() {
		var catalog = require("pilot/plugin_manager").catalog;
		catalog.registerPlugins([ "pilot/index" ]).then(function() {
			var env = require("pilot/environment").create();
			catalog.startupPlugins({ env: env }).then(function() {
				require("fbace/startup").launch(env, options);
				window.onhashchange()
			}).then(onLaunch);
		});
	});
};
startAce()
	</script>
</body>
</html>
